home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / nktools.zip / STRPOS.ASM < prev    next >
Assembly Source File  |  1990-05-08  |  5KB  |  172 lines

  1. ;=======================================================================
  2. ; MODULE NAME:  StrPos.ASM
  3. ; DEPENDENCIES: (None)
  4. ; LAST MOD ON:  9005.08
  5. ; PROGRAMMER:   Naoto Kimura
  6. ;
  7. ;     This is the assembly code for the routines in the StrUtil unit.
  8. ; Many of the routines were rewritten in the hopes that it will not
  9. ; only reduce the memory requirement, but also reduce the execution
  10. ; time.
  11. ;
  12. ;     This file should be assembled with Turbo Assembler.  If you need
  13. ; to use another assembler, then you should keep some things in mind.
  14. ;
  15. ; The TPASCAL memory model sets up automatically:
  16. ; * the standard Turbo Pascal entry code
  17. ;      push bp
  18. ;      mov bp,sp
  19. ; * the standard Turbo Pascal exit code
  20. ;      pop bp
  21. ;      ret n
  22. ; * the order of the arguments don't need to be reversed in the
  23. ;   assembly code.
  24. ;-----------------------------------------------------------------------
  25. ; 9001.20     Naoto Kimura
  26. ;             * Initial version created
  27. ;
  28. ; Modification history:
  29. ;
  30. ; 9002.27     Naoto Kimura
  31. ;             * Corrected code for RightPos.  It used to give the wrong
  32. ;               results.  Rewritten using the SCASB instruction.
  33. ;             * Added function LeftPos which does a similar task as the
  34. ;               RightPos function.
  35. ; 9005.05     Naoto Kimura
  36. ;             * Broke up assembler modules into separate files to try
  37. ;               to decrease overhead (although the overhead is pretty
  38. ;               small, it's always nice to make available whatever
  39. ;               memory the user can use).
  40. ; 9005.05     Naoto Kimura
  41. ;             * Added RPos to the functions rewritten in assembly.
  42. ; 9005.05     Naoto Kimura
  43. ;             * Made minor changes trying to minimize number of
  44. ;               jumps in code.
  45. ;=======================================================================
  46. .MODEL   TPASCAL
  47. LOCALS
  48.  
  49. .CODE
  50.  
  51. ;-----------------------------------------------------------------------
  52. ;FUNCTION RPos( Needle,HayStack : string ) : Byte
  53. ;
  54. ;     This function returns the last matching position of "Needle" in
  55. ; "HayStack."
  56. ;
  57. ; REGISTER USAGE:  AX            -- Return value
  58. ;                  BX,CX,DI,SI,ES    -- Destroyed
  59. ;-----------------------------------------------------------------------
  60. PROC    RPos    FAR    Needle:DWORD,HayStack:DWORD
  61.         PUBLIC    RPos
  62.         USES    ds
  63. ;
  64.         xor    ax,ax
  65.         mov    bx,ax
  66.         les    di,[HayStack]    ;IF Length(HayStack)=0 THEN
  67.         mov    al,[es:di]
  68.         or    al,al
  69.         jz    Done        ;   Return(0)
  70.         ; NOTE: ax <-- Length(HayStack)
  71.         lds    si,[Needle]    ;IF Length(Needle) = 0 THEN
  72.         mov    bl,[ds:si]
  73.         or    bl,bl
  74.         jz    Done        ;   Return(Length(HayStack))
  75.         ; NOTE: bx <-- Length(Needle)
  76.         cmp    al,bl        ;IF Length(Needle) > Length(HayStack) THEN
  77.         jl    @@NotFound
  78.         mov    cx,ax        ;  cx <-- length(HayStack)
  79.         sub    cx,bx        ;         - length(Needle)
  80.         inc    cx        ;         + 1
  81.         add    di,cx        ;  di <-- ofs(HayStack[cx])
  82.                     ;  REPEAT
  83. @@More:        std            ;    reverse string op
  84.         mov    al,[si+1]    ;    ax <-- Needle[1]
  85.         repne scasb
  86.         je    @@Ok        ;    if not found then
  87. @@NotFound:    mov    ax,0        ;        Return(0)
  88.         jmp    Done
  89. @@Ok:        mov    ax,cx
  90.         mov    dx,di
  91.         inc    di        ;    adjust di
  92.         inc    si        ;    move ds:si to 1st char
  93.         cld            ;    forward string op
  94.         mov    cx,bx
  95.         repz cmpsb        ;    test match
  96.         jz    @@Found        ;
  97.         lds    si,[Needle]
  98.         mov    di,dx
  99.         mov    cx,ax
  100.         or    cx,cx        ;  UNTIL cx=0
  101.         jnz    @@More
  102.                     ;END IF
  103. @@Found:    sub    di,bx        ;adjust di
  104.         mov    ax,di
  105.         les    di,[HayStack]
  106.         sub    ax,di
  107. Done:        ret
  108.     ENDP
  109.  
  110. ;-----------------------------------------------------------------------
  111. ;FUNCTION RightPos ( S:String; C:Char ) : Integer
  112. ;
  113. ;     This function returns the last matching position of character
  114. ; "C" in "S".
  115. ;
  116. ; REGISTER USAGE:  AX        -- Return value
  117. ;                  BX,CX,DI,ES    -- Destroyed
  118. ;-----------------------------------------------------------------------
  119. RightPos    PROC FAR S:DWORD,C:BYTE
  120.         PUBLIC    RightPos
  121.         USES    DS
  122.         les    di,[S]        ; get addr of function parameter
  123.         xor    ax,ax
  124.         mov    al,[es:di]
  125.         or    ax,ax
  126.         jz    @@done
  127.         mov    bx,di
  128.         dec    bx
  129.         mov    cx,ax
  130.         add    di,cx
  131.         mov    al,[C]
  132.         std            ; backward string op
  133.         repne scasb        ; search
  134.         je    @@Found
  135.         mov    di,bx
  136. @@Found:    xchg    ax,di
  137.         sub    ax,bx
  138. @@done:        ret
  139. RightPos    ENDP
  140.  
  141. ;-----------------------------------------------------------------------
  142. ;FUNCTION LeftPos ( S:String; C:Char ) : Integer
  143. ;
  144. ;     This function returns the first matching position of character
  145. ; "C" in "S".
  146. ;
  147. ; REGISTER USAGE:  AX        -- Return value
  148. ;                  BX,CX,DI,ES    -- Destroyed
  149. ;-----------------------------------------------------------------------
  150. LeftPos        PROC FAR S:DWORD,C:BYTE
  151.         PUBLIC    LeftPos
  152.         USES    DS
  153.         xor    ax,ax
  154.         les    di,[S]        ; get addr of function parameter
  155.         mov    al,[es:di]
  156.         or    ax,ax
  157.         jz    @@done
  158.         inc    di
  159.         mov    bx,di
  160.         mov    cx,ax
  161.         mov    al,[C]
  162.         cld            ; forward string op
  163.         repne scasb        ; search
  164.         je    @@Found
  165.         mov    di,bx
  166. @@Found:    xchg    ax,di
  167.         sub    ax,bx
  168. @@done:        ret
  169. LeftPos        ENDP
  170.  
  171. END
  172.